package com.app.teacherActivity.fragment;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.app.R;
import com.app.bean.ChatBean;
import com.app.util.AdapterUtil;
import com.app.util.DBHelper;
import com.app.util.DBHelper2;

import java.util.ArrayList;
import java.util.List;

public class StudentFragment extends Fragment {

    private RecyclerView recy;
    private EditText commentEdit;
    private AppCompatButton send;
    private int id;
    private String username;
    private List<ChatBean> list = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_student2, container, false);
        id = getActivity().getIntent().getIntExtra("id", 0);
        initView(view);
        getData();
        setAdapter();

        send.setOnClickListener(v -> {


            DBHelper2 dbHelper2 = new DBHelper2(getContext());
            SQLiteDatabase db = dbHelper2.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("user_id", id);
            values.put("username", username);
            values.put("content", commentEdit.getText().toString());
            long result = db.insert("chat", null, values);
            if (result != -1) {
                Toast.makeText(getContext(), "发送成功", Toast.LENGTH_SHORT).show();
                commentEdit.setText("");
                setAdapter();
            } else {
                Toast.makeText(getContext(), "发送失败", Toast.LENGTH_SHORT).show();
            }
            db.close();
        });

        return view;
    }

    private void setAdapter() {
        list.clear();
        DBHelper2 dbHelper = new DBHelper2(getContext());
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("chat", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            int userId = cursor.getInt(cursor.getColumnIndex("user_id"));
            String userName = cursor.getString(cursor.getColumnIndex("username"));
            String Content = cursor.getString(cursor.getColumnIndex("content"));
            list.add(new ChatBean(userId, userName, Content));
        }
        dbHelper.close();
        db.close();
        cursor.close();

        AdapterUtil<ChatBean> adapter = new AdapterUtil<>(R.layout.char_item01, ((data, position, holder) -> {
            LinearLayout layout1 = holder.getView(R.id.layout1);
            TextView username1 = holder.getView(R.id.username1);
            TextView content1 = holder.getView(R.id.content1);
            LinearLayout layout2 = holder.getView(R.id.layout2);
            TextView username2 = holder.getView(R.id.username2);
            TextView content2 = holder.getView(R.id.content2);
            if (data.getUserId() == id) {
                // 号主发的消息
                layout1.setVisibility(View.GONE);
                layout2.setVisibility(View.VISIBLE);
                content2.setText(data.getCotent());
                username2.setText(data.getUserName());
            } else {
                layout1.setVisibility(View.VISIBLE);
                layout2.setVisibility(View.GONE);
                content1.setText(data.getCotent());
                username1.setText(data.getUserName());
            }
        }));
        adapter.listUpdate(list);
        recy.setAdapter(adapter);
    }

    private void getData() {
        DBHelper dbHelper = new DBHelper(getContext());
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sqlQuery = "SELECT * FROM user WHERE id = ?";
        Cursor cursor = db.rawQuery(sqlQuery, new String[]{String.valueOf(id)});
        if (cursor.moveToFirst()) {
            username = cursor.getString(cursor.getColumnIndex("username"));
        }
        dbHelper.close();
        cursor.close();
    }

    private void initView(View view) {
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setVisibility(View.GONE);
        ((TextView) toolbar.getChildAt(1)).setText("聊天");
        recy = (RecyclerView) view.findViewById(R.id.recy);
        recy.setLayoutManager(new LinearLayoutManager(getContext()));
        commentEdit = (EditText) view.findViewById(R.id.comment_edit);
        send = (AppCompatButton) view.findViewById(R.id.send);
    }
}